package com.itheima.controller;

import com.itheima.entity.OperateLog;
import com.itheima.service.OperateLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OperateLogController {

    @Autowired
    private OperateLogService operateLogService;

    @GetMapping("/log/page")
    public Map<String, Object> getLogPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        List<OperateLog> logs = operateLogService.getLogPage(page, pageSize);
        long total = operateLogService.getTotalLogs();

        Map<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("msg", null);

        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("rows", logs);
        response.put("data", data);

        return response;
    }

    // 示例：插入日志的接口
    @PostMapping("/log/insert")
    public Map<String, Object> insertLog(
            @RequestBody OperateLog log) {
        operateLogService.insert(log);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("msg", "success");
        response.put("data", null);

        return response;
    }
}